﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.produceDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.produceBLL
{
   public  class machineTableBLL
    {

       private machineTableTableAdapter _machineTableAdapter = null;
       private machineTableTableAdapter machineTableAdapter
        {
            get
            {
                if (_machineTableAdapter == null) _machineTableAdapter = new machineTableTableAdapter();
                return _machineTableAdapter;
            }
        }
        /// <summary>
        /// 查询客户类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public produceDataSet.machineTableDataTable getmachineTableAll()
        {
            try
            {
                return machineTableAdapter.GetDatamachineTableAll ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据客户类别查询客户类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public produceDataSet.machineTableDataTable getmachineTableAll(string machineNo)
        {
            try
            {
                return machineTableAdapter.GetDatamachineTableBymachineNo(machineNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 根据客户类别查询客户类别表
       /// </summary>
       /// <param name="row"></param>
       /// <returns></returns>
       public produceDataSet.machineTableDataTable getmachineTableAllBypname(string pname)
       {
           try
           {
               return machineTableAdapter.GetDatamachineTableBypanme (pname);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
        /// 插入客户类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int machineTableInsert(produceDataSet.machineTableDataTable machineTable)
        {
            machineTableModel machineRow = new machineTableModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;

            try
            {
                tn = conn.BeginTransaction();
                machineTableDAL machine = new machineTableDAL(tn, conn);
                foreach (produceDataSet.machineTableRow detailmachineRow in machineTable)
                {
                    if (detailmachineRow.RowState != DataRowState.Deleted)
                    {

                  
                            machineRow.machineNo = detailmachineRow.machineNo;
                 

                        if (!detailmachineRow.IsmachinetypeNull ())
                        {
                            machineRow.machinetype = detailmachineRow.machinetype;
                        }

                        else { machineRow.machinetype = ""; }
                        if (!detailmachineRow.IspanmeNull ())
                        {
                            machineRow.panme = detailmachineRow.panme;
                        }

                        else { machineRow.panme = ""; }
                        if (!detailmachineRow.IspwNull ())
                        {
                            machineRow.pw = detailmachineRow.pw;
                        }

                        else { machineRow.pw = ""; }
                        if (!detailmachineRow.IsbillStatusNull ())
                        {
                            machineRow.billStatus = detailmachineRow.billStatus;
                        }

                        else { machineRow.billStatus = false ; }
                    }

                    //编辑子表记录,新增
                    if (detailmachineRow.RowState == DataRowState.Added)
                    {

                        machine.machineTableInsert(machineRow);
                        //string orderSerial = basicSql.getOrderSerail("漂染单号");
                        //basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "漂染单号");


                    }
                    //修改
                    if (detailmachineRow.RowState == DataRowState.Modified)
                    {
                        machine.machineTableUpdate (machineRow);
                    }
                    //删除
                    if (detailmachineRow.RowState == DataRowState.Deleted)
                    {
                        //DataRow delrow = (DataRow)detailmachineRow;
                        //machine.machineTableDel(delrow["recordid", DataRowVersion.Original].ToString());
                    }


                }
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("已经存在主键");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新客户信息表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int machineTableUpdate(machineTableModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                machineTableDAL machineTable = new machineTableDAL(tn, conn);
                machineTable.machineTableUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除一行客户类别表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int machineTableDel(string recordid)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                machineTableDAL machineTable = new machineTableDAL(tn, conn);
                machineTable.machineTableDel(recordid);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
